package com.bbms.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.bbms.dao.ManagerDao;
import com.bbms.entity.Manager;
import com.bbms.entity.Page;
import com.bbms.util.HibernateUtil;
import com.bbms.util.StringUtil;

/**
 * 管理员DAO层接口实现类
 * @author Administrator
 */
@Repository("ManagerDaoImpl")
public class ManagerDaoImpl implements ManagerDao{

	/*
	 * 添加管理员
	 * @see com.bbms.dao.BaseDAO#save(java.lang.Object)
	 */
	@Override
	public void save(Manager t) {
		HibernateUtil.save(t);
	}

	/*
	 * 更改管理员信息
	 * @see com.bbms.dao.BaseDAO#update(java.lang.Object)
	 */
	@Override
	public void update(Manager t) {
		
		StringBuffer sql=new StringBuffer();
		sql.append( "UPDATE bbms_user_manager  set  category="+ t.getCategory() +" "
				  + "WHERE "
				  + "user_info_id=" + t.getUserInfoId() + " ");
		
		System.out.println("更改Manager的sql:  "+sql.toString());
		
		HibernateUtil.executeSqlUpdate(sql.toString());
	}

	/*
	 * 删除管理员
	 * @see com.bbms.dao.BaseDAO#delete(java.lang.Object)
	 */
	@Override
	public void delete(Manager t) {
		HibernateUtil.delete(t);
	}

	/*
	 * 获取分页数据（无查询）
	 * @see com.bbms.dao.BaseDAO#getPage(int, int)
	 */
	@Override
	public Page getPage(int currentPage, int pageSize) {
	
		return null;
	}

	/*
	 * 获取全部信息（无分页）
	 * @see com.bbms.dao.BaseDAO#query()
	 */
	@Override
	public List<?> query() {
	
		return null;
	}

	/*
	 * 获取分页数据（包含查询）
	 * @see com.bbms.dao.ManagerDao#getPage(int, int, java.lang.String, java.lang.String, java.lang.String, java.lang.Integer, java.lang.String, java.lang.String, java.lang.String)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public Page getPage(int currentPage, int pageSize, String s_name, String s_account, String s_phone,
			Integer s_categoryId, String IDCard, String start_time, String end_time) {
				
		StringBuffer sql=new StringBuffer();
		StringBuffer count=new StringBuffer();
		
		sql.append(   "SELECT M.id AS id, M.user_info_id AS userInfoId, M.category AS category, U.`name` AS `name`, "
					+ "U.account AS account, U.IDCard AS IDCard, U.phone AS phone,U.birthday AS birthday "
					+ "FROM bbms_user_manager M "
					+ "LEFT JOIN bbms_user_info U "
					+ "ON M.user_info_id=U.id "
					+ "WHERE 1=1 ");
		
		count.append("SELECT COUNT(*) AS count "
					+ "FROM bbms_user_manager M "
					+ "LEFT JOIN bbms_user_info U "
					+ "ON M.user_info_id=U.id "
					+ "WHERE 1=1 ");
		
		if (!StringUtil.isEmpty(s_name)) {
			sql.append(" AND U.name LIKE '%"+ s_name +"%'");
			count.append(" AND U.name LIKE '%"+ s_name +"%'");
		}
		if (!StringUtil.isEmpty(s_account)) {
			sql.append(" AND U.account LIKE '%"+ s_account +"%'");
			count.append(" AND U.account LIKE '%"+ s_account +"%'");
		}
        if (!StringUtil.isEmpty(s_phone)) {
        	sql.append(" AND U.phone LIKE '%"+ s_phone +"%'");
        	count.append(" AND U.phone LIKE '%"+ s_phone +"%'");
		}
        if (s_categoryId!=null&&s_categoryId!=0) {
        	sql.append(" AND M.category ="+ s_categoryId +"");
        	count.append(" AND M.category ="+ s_categoryId +"");
		}
        if (!StringUtil.isEmpty(IDCard)) {
        	sql.append(" AND U.IDCard LIKE '%"+ IDCard +"%'");
        	count.append(" AND U.IDCard LIKE '%"+ IDCard +"%'");
		}
        if (!StringUtil.isEmpty(start_time)) {
        	sql.append(" AND UNIX_TIMESTAMP(U.birthday) >=UNIX_TIMESTAMP('" + start_time + "')");
        	count.append(" AND UNIX_TIMESTAMP(U.birthday) >=UNIX_TIMESTAMP('" + start_time + "')");
		}
        if (!StringUtil.isEmpty(end_time)) {
        	sql.append(" AND UNIX_TIMESTAMP(U.birthday) <=UNIX_TIMESTAMP('" + end_time + "')");
        	count.append(" AND UNIX_TIMESTAMP(U.birthday) <=UNIX_TIMESTAMP('" + end_time + "')");
        }
        sql.append(" LIMIT " + ((currentPage - 1)* pageSize) + "," + pageSize +" ");
        
        System.out.println("sql语句：	"+sql.toString());
        
        Page page=new Page();
        List<?> list=HibernateUtil.executeSqlQuery(sql.toString());
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setRows(list);
        
        List<HashMap> list2=(List<HashMap>)HibernateUtil.executeSqlQuery(count.toString());
        page.setTotal(Integer.parseInt(list2.get(0).get("count").toString()));
        
		return page;
	}

}
